package com.tensquare.article.controller;

import com.tensquare.article.pojo.Channel;
import com.tensquare.article.service.ChannelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Alfred
 * @description TODO
 * @date 2021/4/26 19:11
 */
@RestController
@CrossOrigin
@RequestMapping("/channel")
public class ChannelController {

    @Autowired
    private ChannelService channelService;

    /**
     * fetch data by rule id
     *
     * @description  增加频道
     * @param
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Channel channel){
        channelService.add(channel);
        return new Result(true, StatusCode.OK,"增加成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  频道全部列表
     * @param
     * @return
     */
    @GetMapping
    public Result findAll(){
        return new Result(true,StatusCode.OK,"查询成功",channelService.findAll());
    }

    /**
     * fetch data by rule id
     *
     * @description  根据ID查询频道
     * @param
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable String id){
        return new Result(true,StatusCode.OK,"查询成功",channelService.findById(id));
    }

    /**
     * fetch data by rule id
     *
     * @description  修改频道
     * @param
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable String id,@RequestBody Channel channel){
        channel.setId(id);
        channelService.update(channel);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  根据ID删除频道
     * @param
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id){
        channelService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  根据条件查询频道列表
     * @param
     * @return
     */
    @PostMapping("/search")
    public Result findSearch(@RequestBody Map searchMap){
        return new Result(true,StatusCode.OK,"查询成功",channelService.findSearch(searchMap));
    }


    /**
     * fetch data by rule id
     *
     * @description  频道分页
     * @param
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    public Result findSearch(@RequestBody Map searchMap,@PathVariable int page,@PathVariable int size){
        Page<Channel> pageList = channelService.findSearch(searchMap, page, size);
        return new Result(true,StatusCode.OK,"查询成功",new PageResult<>(pageList.getTotalElements(),pageList.getContent()));
    }

}
